package com.tour.controller;


import com.tour.config.ResponseWrapper;
import com.tour.entity.Discuss;
import com.tour.entity.User;
import com.tour.service.impl.DiscussServiceImpl;
import com.tour.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lel
 * @since 2020-12-30
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/app/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @ApiOperation("按照旧密码修改新密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id"),
            @ApiImplicitParam(name = "newPassword",value = "新密码")
    })
    @PutMapping("/updatePassword")
    public ResponseWrapper updatePassword(Integer userId,String newPassword){
        ResponseWrapper wrapper = userService.updatePassword(userId,newPassword);
        return wrapper;
    }
    @ApiOperation("查询一个用户信息")
    @ApiImplicitParam(name = "userId" ,value = "用户自己的id")
    @GetMapping("/findUserById")
    public ResponseWrapper findUserById(@RequestParam(name = "userId",required = true)Integer userId){
        ResponseWrapper wrapper = userService.findUserById(userId);
        return wrapper;
    }
    @ApiOperation("导出excel")
    @GetMapping("/excel")
    public void excel( HttpServletResponse response)throws IOException {
        userService.excel(response);
        return;
    }
    @ApiOperation("模糊查询或查询所有用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content",value = "搜索框内容"),
            @ApiImplicitParam(name = "pageSize",value = "每页展示的数据条数",defaultValue = "10"),
            @ApiImplicitParam(name = "pageNum",value = "页码数",defaultValue = "1")
    })
    @GetMapping("/findAllAdmin")
    public ResponseWrapper findAllAdmin(@RequestParam(name = "content",required = false)String content,
                                        @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,
                                        @RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum){
        ResponseWrapper wrapper = userService.findAllUser(content,pageNum,pageSize);
        return wrapper;
    }
    @ApiOperation("增加一个用户")
    @PostMapping("/addUser")
    public ResponseWrapper addUser(@RequestBody User user){
        ResponseWrapper wrapper = userService.addUser(user);
        return wrapper;
    }
    @ApiOperation("修改用户信息")
    @PutMapping("/updateUser")
    public ResponseWrapper updateUser(@RequestBody User user){
        ResponseWrapper wrapper = userService.updateUser(user);
        return wrapper;
    }
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "userId",value = "用户id")
    @DeleteMapping("/delUser")
    public ResponseWrapper delUSer(@RequestParam(name = "userId")Integer userId){
        ResponseWrapper wrapper = userService.delUser(userId);
        return wrapper;
    }
    @ApiOperation("微信授权登录")
    @GetMapping("/wechatlogin")
    public ResponseWrapper wechatlogin(){
        ResponseWrapper wrapper = userService.wechatlogin();
        return wrapper;
    }
    @ApiOperation("获取用户微信登录返回信息")
    @ApiImplicitParam(name = "code",value = "微信授权返回的code")
    @PutMapping("/getUserInfo")
    public ResponseWrapper getUserInfo(@RequestParam(name = "code")String code) throws UnsupportedEncodingException {
        ResponseWrapper wrapper = userService.getUserInfo(code);
        return wrapper;
    }
    @ApiOperation("判断token是否有效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token",value = "获取的token"),
            @ApiImplicitParam(name = "openid",value = "用户的openid")
    })
    @GetMapping("/istoken")
    public ResponseWrapper istoekn(@RequestParam(name = "access_token")String access_token,
                                   @RequestParam(name = "openid")String openid){
        ResponseWrapper wrapper = userService.istoken(access_token,openid);
        return wrapper;
    }


}

